package cn.gan.loveofsoul.commons.utils;

//import org.apache.commons.lang.ObjectUtils;

import java.util.*;

/**
 * 继承自Spring util的工具类，减少jar依赖
 *
 * @Description:
 * @Author: Jayh
 * @CreateTime: 2017年7月31日 下午12:16:53
 */
public class StringUtils extends org.springframework.util.StringUtils {

    /**
     * Check whether the given {@code CharSequence} contains actual <em>text</em>.
     * <p>More specifically, this method returns {@code true} if the
     * {@code CharSequence} is not {@code null}, its length is greater than 0, and it contains at least one non-whitespace
     * character.
     * <p><pre class="code">
     * StringUtils.isBlank(null) = true
     * StringUtils.isBlank("") = true
     * StringUtils.isBlank(" ") = true
     * StringUtils.isBlank("12345") = false
     * StringUtils.isBlank(" 12345 ") = false
     * </pre>
     *
     * @param str the {@code CharSequence} to check (may be {@code null})
     * @return {@code true} if the {@code CharSequence} is not {@code null}, its length is greater than 0, and it does not contain
     * whitespace only
     * @see Character#isWhitespace
     */
    public static boolean isBlank(final CharSequence cs) {
        
        return !StringUtils.isNotBlank(cs);
    }
    
    /**
     * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
     *
     * <pre>
     * StringUtils.isNotBlank(null)      = false
     * StringUtils.isNotBlank("")        = false
     * StringUtils.isNotBlank(" ")       = false
     * StringUtils.isNotBlank("bob")     = true
     * StringUtils.isNotBlank("  bob  ") = true
     * </pre>
     *
     * @param cs the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is not empty and not null and not whitespace
     * @see Character#isWhitespace
     */
    public static boolean isNotBlank(final CharSequence cs) {
        
        return StringUtils.hasText(cs);
    }
    
    /**
     * Convert a {@code Collection} into a delimited {@code String} (e.g. CSV).
     * <p>Useful for {@code toString()} implementations.
     *
     * @param coll the {@code Collection} to convert
     * @param delim the delimiter to use (typically a ",")
     * @return the delimited {@code String}
     */
    public static String join(Collection<?> coll, String delim) {
        
        return StringUtils.collectionToDelimitedString(coll, delim);
    }
    
    /**
     * Convert a {@code String} array into a delimited {@code String} (e.g. CSV).
     * <p>Useful for {@code toString()} implementations.
     *
     * @param arr the array to display
     * @param delim the delimiter to use (typically a ",")
     * @return the delimited {@code String}
     */
    public static String join(Object[] arr, String delim) {
        
        return StringUtils.arrayToDelimitedString(arr, delim);
    }
    
    /**
     * 生成uuid
     *
     * @return UUID
     */
    public static String getUUId() {
        //return UUID.randomUUID().toString();
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        // 去掉"-"符号
        String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23)
                + str.substring(24);
        return temp;
    }
    
    /**
     * 数字补0
     *
     * @param num
     * @param length
     * @return
     */
    public static String compWithZore(Integer num, Integer length) {
        
        String str = null;
        if (num != null) {
            str = String.format("%0" + length + "d", num);
        }
        return str;
    }
    
    /**
     * 获取任意长度的随机数
     *
     * @param length
     * @return
     */
    public static int random(int length) {
        
        return (int) ((Math.random() * 9 + 1) * Math.pow(10, length - 1));
    }
    
    public static void main(String args[]) {
        
        System.out.println(random(5));
    }

    /**
     * 功能描述: 姓名脱敏处理
     * 脱敏规则：只显示第一个汉字,比如李某某置换为李**, 李某置换为李*
     *
     * @param custName 完整姓名
     * @return java.lang.String
     * @author ganwh 2023/3/15 10:22
     */
    public static String custNameEncrypt(String custName) {
        if (StringUtils.isNotBlank(custName)) {
            String name = org.apache.commons.lang3.StringUtils.left(custName, 1);
            return org.apache.commons.lang3.StringUtils.rightPad(name, org.apache.commons.lang3.StringUtils.length(custName), "*");
        }
        return custName;
    }

    /**
     * 功能描述: 身份证号脱敏处理 前6后4
     *
     * @param identity 身份证号
     * @return java.lang.String
     * @author douhm 2022/10/8 14:29
     */
    public static String certNumEncrypt(String identity) {
        if (StringUtils.isEmpty(identity) || (identity.length() != 18)) {
            return identity;
        }
        return identity.replaceAll("(\\w{6})\\w*(\\w{4})", "$1****$2");
    }

}
